package cn.com.cetccst.vpn.verdor;

import android.content.Context;
import android.text.TextUtils;

import java.util.Locale;

import cn.com.cetccst.vpn.callback.ILoginCallback;
import cn.com.cetccst.vpn.callback.IServerStatusCallback;
import cn.com.cetccst.vpn.callback.IVpnEventCallback;
import cn.com.cetccst.vpn.VPNConstants;

public abstract class AbsVpn implements IVpn {

    protected static String TAG = "AbsVpn";

    protected ILoginCallback mLoginCallback;
    protected IVpnEventCallback mVpnEventCallback;
    protected IServerStatusCallback mServerStatusCallback;

    protected String mServerAddr;
    protected int mPort;
    protected String mAlias;
    protected Context mContext;
    protected String mPinCode;

    protected int mEventStatus = VPNConstants.VPN_STATUS.VPN_STATUS_LOGOUT;
    protected int mServerStatus = VPNConstants.VPN_STATUS.VPN_STATUS_PENDING;

    protected abstract void initServer();

    //将VPN SDK 返回的状态信息 mapping 为自定义的code
    protected abstract int mappingCode(int code);

    protected abstract int getErrorType(int code, String message);

    @Override
    public void setLoginCallback(ILoginCallback loginCallback) {
        mLoginCallback = loginCallback;
    }

    @Override
    public void setVpnEventCallback(IVpnEventCallback eventCallback) {
        mVpnEventCallback = eventCallback;
    }

    @Override
    public void setServerStatusCallback(IServerStatusCallback serverStatusCallback) {
        mServerStatusCallback = serverStatusCallback;
    }

    @Override
    public void setServerInfo(String addr, int port, String alias) {
        mServerAddr = addr;
        mPort = port;
        mAlias = alias;
    }

    protected void event(int event) {
        mEventStatus = mappingCode(event);
        if (mVpnEventCallback != null) {
            mVpnEventCallback.onVpnEvent(event);
        }
    }

    protected boolean valideServerInfo() {
        boolean success = !TextUtils.isEmpty(mServerAddr) && mPort >= 0;
        if (mLoginCallback != null && !success) {
            mLoginCallback.onLoginFailed(-10000, String.format(Locale.ENGLISH, "valide server info failed for [server:%s,port:%d]", mServerAddr, mPort));
        }
        return success;
    }

    protected boolean valideArgs(String... args) {
        boolean success = true;
        for (String arg : args) {
            success = success && !TextUtils.isEmpty(arg);
        }
        return success;
    }

    protected void log(String msg) {
        log(msg, null);
    }

    protected void log(String msg, Throwable throwable) {
//        Logger.log(Log.INFO, TAG, msg, throwable);
    }
}
